import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:jyofservice/YunXinGongKuang/AddWorkCondition.dart';
import 'package:jyofservice/YunXinGongKuang/LevelTree.dart';
import 'package:jyofservice/bean/work_condition_entity.dart';
import 'package:jyofservice/main.dart';
import 'package:jyofservice/util/AesUtil.dart';
import 'package:jyofservice/util/SlideRoute.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../util/webconfig.dart';
import 'NewsDetail.dart';

class NoticePage extends StatefulWidget {
  @override
  _NoticePageState createState() => _NoticePageState();
}

class _NoticePageState extends State<NoticePage> {
  bool isloading = false;
  List<WorkConditionEntity> posts = [];
  bool ifAdd = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    init().then((value){
      switch (value) {
        case 1: //客服主管领导
          ifAdd = false;
          break;
        case 2: //客服人员
          ifAdd = true;
          break;
        case 3:
          ifAdd = false;
          break;
        case 4:
          ifAdd = true;;
          break;
      }
      getWorkCondition("GetMyDepartmentWorkInfo");
    });

  }
  Future init() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    var postid = preferences.getInt("postid");
    return postid;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(WebConfig().ColorValue),
      appBar: AppBar(
        title: Text(
          '运行工况',
          style:
          TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(40)),
        ),
        centerTitle: true,
        actions: <Widget>[
          Offstage(
            offstage: ifAdd,
            child:TextButton(
              child: Icon(Icons.add,color: Colors.white,),
              onPressed: (){
                Navigator.of(context)
                    .push(SlideRoute().createRoute(AddWorkConfition(index: 0))).then((value){
                  if(value!=null&&value) {
                    getWorkCondition("GetMyDepartmentWorkInfo");
                  }
                });
              },
            ) ,
          )

        ],
      ),
      body: Stack(
        children: <Widget>[
          Column(
            children: <Widget>[
              /*
              SizedBox(
                height: ScreenUtil().setHeight(5),
              ),
              Container(
                width: ScreenUtil().setWidth(600),
                height: ScreenUtil().setHeight(80),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(50),
                    border: Border.all(
                        color: Colors.blue,
                        width: ScreenUtil().setWidth(2))),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: GestureDetector(
                        onTap: () {
                          getWorkCondition("GetMyDepartmentWorkInfo");
                        },
                        child: Container(

                          decoration: BoxDecoration(
                              color: Colors.blue,
                              borderRadius: BorderRadius.only(topLeft: Radius.circular(50),bottomLeft:  Radius.circular(50),)
                          ),
                          child: Center(
                            child: Text(
                              '生效',
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: ScreenUtil().setSp(36)),
                            ),
                          ),
                        ),
                      ),
                    ),
                    Container(
                      width: ScreenUtil().setWidth(2),
                      height: ScreenUtil().setHeight(100),
                      color: Colors.blue,
                    ),
                    Expanded(
                      flex: 1,
                      child: GestureDetector(
                        onTap: () {
                          getWorkCondition("GetMyDepartmentWorkInfoSX");
                        },
                        child: Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.only(topRight: Radius.circular(50),bottomRight:  Radius.circular(50),)
                          ),
                          child: Center(
                            child: Text(
                              '逾期',
                              style: TextStyle(
                                  color: Colors.blue,
                                  fontSize: ScreenUtil().setSp(36)),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              */
              SizedBox(
                height: ScreenUtil().setHeight(5),
              ),
              Expanded(child: ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return getRowData(context, index);
                },
                itemCount: posts.length,
              ))
            ],
          ),
          Offstage(
            offstage: isloading,
            child: SpinKitFadingCircle(
              color: Colors.blue,
              size: 50.0,
            ),
          )
        ],
      ),
    );
    ;
  }

  Widget getRowData(BuildContext context, int index) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).push(SlideRoute().createRoute(NewsDetail(title: posts[index].title ,stateName: posts[index].stateName,neirong: posts[index].neiRong,enddt: posts[index].endDt,ljstoptime: posts[index].lJStopTime,fanwei: posts[index].fanWeiName,createdate: posts[index].createDate,realname: posts[index].realName,workConditionEntity: posts[index],))).then((value){
          if(value!=null&&value) {
            getWorkCondition("GetMyDepartmentWorkInfo");
          }
        });
      },
      child: Container(
          height: ScreenUtil().setHeight(200),
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 10),
          padding: EdgeInsets.fromLTRB(10,5,10,5),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                posts[index].title!,
                softWrap: false,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                    color: Colors.black,
                    fontSize: ScreenUtil().setSp(35)),
              ),
              Text(
                posts[index].createDate!,
                style: TextStyle(
                    color: Colors.black54,
                    fontSize: ScreenUtil().setSp(25)),
              ),
              SizedBox(
                height: ScreenUtil().setHeight(5),
              ),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Image.asset(
                    "images/person.png",
                    width: ScreenUtil().setWidth(70),
                  ),
                  Expanded(
                      flex: 1,
                      child: Text(
                        posts[index].realName!,
                        style: TextStyle(
                            color: Colors.black54,
                            fontSize: ScreenUtil().setSp(30)),
                      )),
                ],
              ),
            ],
          )),
    );
  }

  Future getWorkCondition(String url) async {
    posts.clear();
    if (mounted) {
      setState(() {
        isloading = false;
      });
    }
    posts.clear();
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    String aesData = await AesUtil.encodeBase64();
    var data = {"key": key, "ClientType": "phone", "ClientKey": aesData};
    print('==运行工况 ${WebConfig().baseUrl + "DB/"+url}');
    Response response = await Dio().post(
        WebConfig().baseUrl + "DB/"+url,
        data: data);
    print('==运行工况返回 ${response.data}');
    if (mounted) {
      setState(() {
        for (int i = 0; i < response.data["Data"].length; i++) {
          posts.add(new WorkConditionEntity.fromJson(response.data["Data"][i]));
        }
        isloading = true;
      });
    }
    return response.data;
  }
}
